<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="平台名称" prop="pfCode">
        <el-select v-model="queryParams.pfCode" placeholder="请选择平台名称" clearable>
          <el-option
              v-for="item in platformOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入openId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsId">
        <el-select v-model="queryParams.goodsId" placeholder="请选择商品" clearable style="width: 100%">
          <el-option
              v-for="item in goodsOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="购买数量" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入购买数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="payAmount">
        <el-input
          v-model="queryParams.payAmount"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量KG" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量KG"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" multiple placeholder="请选择订单状态">
          <el-option
              v-for="item in orderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:orders:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="150"/>
      <el-table-column label="商品名称" align="center" prop="goodsName" width="150" show-overflow-tooltip/>
      <el-table-column label="支付金额" align="center" prop="payAmount" />
      <el-table-column label="平台code" align="center" prop="pfCode" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="重量KG" align="center" prop="weight" />
      <el-table-column label="订单类型" align="center" prop="type" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="用户id" align="center" prop="userId" />-->
      <el-table-column label="openId" align="center" prop="openId" width="150" show-overflow-tooltip/>
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="商品价格" align="center" prop="goodsPrice" />
      <el-table-column label="购买数量" align="center" prop="num" />
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="用户手机号" align="center" prop="phone" width="100px" show-overflow-tooltip/>
      <el-table-column label="用户地址" align="center" prop="address" width="100" show-overflow-tooltip/>
      <el-table-column label="系统用户id" align="center" prop="sysUserId" width="150"/>
      <el-table-column label="下单时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>xs
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orders:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orders:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入订单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择订单类型" style="width: 100%">
                <el-option
                    v-for="dict in dict.type.order_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="用户id" prop="userId">-->
<!--              <el-input v-model="form.userId" placeholder="请输入用户id" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="openId" prop="openId">
              <el-input v-model="form.openId" placeholder="请输入openId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickname">
              <el-input v-model="form.nickname" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户头像" prop="avatar">
              <el-input v-model="form.avatar" placeholder="请输入用户头像" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品id" prop="goodsId">
              <el-input v-model="form.goodsId" placeholder="请输入商品id" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="goodsName">
              <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品价格" prop="goodsPrice">
              <el-input v-model="form.goodsPrice" placeholder="请输入商品价格" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="购买数量" prop="num">
              <el-input v-model="form.num" placeholder="请输入购买数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付金额" prop="payAmount">
              <el-input v-model="form.payAmount" placeholder="请输入支付金额" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="重量KG" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入重量KG" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择订单状态" style="width: 100%">
                <el-option
                    v-for="dict in dict.type.order_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入用户姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入用户手机号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入用户地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统用户id" prop="sysUserId">
              <el-input v-model="form.sysUserId" placeholder="请输入系统用户id" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/pets/orders";
import {listPlatform} from "@/api/pets/platform";
import {listGoods} from "@/api/pets/goods";

export default {
  name: "Orders",
  dicts: ['order_status', 'order_type'],
  data() {
    return {
      orderOptions: [{
        value: 0,
        label: '未支付'
      }, {
        value: 1,
        label: '已支付'
      }, {
        value: 2,
        label: '已发货'
      }, {
        value: 3,
        label: '已收货'
      }],
      platformOptions: [],
      goodsOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        type: null,
        openId: null,
        nickname: null,
        goodsName: null,
        pfCode: null,
        num: null,
        payAmount: null,
        weight: null,
        status: null,
        goodsId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        goodsPrice: [
          { required: true, message: "商品价格不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "购买数量不能为空", trigger: "blur" }
        ],
        payAmount: [
          { required: true, message: "支付金额不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "重量KG不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态 0-未支付 1-已支付 2-已发货 3-已收货不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    const goodsId = this.$route.params && this.$route.params.goodsId;
    const pfCode = this.$route.params && this.$route.params.pfCode;
    console.log("goodsId:", goodsId)
    console.log("pfCode:", pfCode)
    if (goodsId && pfCode){
      this.queryParams.goodsId = goodsId
      this.queryParams.pfCode = pfCode
      this.queryParams.status = [1,2,3]
    }
    this.getList();
    this.getPlatformOption();
    this.getGoodsOption();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        type: null,
        userId: null,
        openId: null,
        nickname: null,
        avatar: null,
        goodsId: null,
        goodsName: null,
        goodsPrice: null,
        num: null,
        payAmount: null,
        weight: null,
        status: null,
        name: null,
        phone: null,
        address: null,
        sysUserId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    },

    getPlatformOption(){
      listPlatform({pageNum:1, pageSize:1000}).then(response=>{
        this.platformOptions = response.rows.map(platform=>{
          return {
            "value": platform.pfCode,
            "label": platform.name
          }
        })
      })
    },

    getGoodsOption(){
      listGoods().then(response=>{
        this.goodsOptions = response.rows.map(goods=>{
          return {
            "value": goods.id,
            "label": goods.name
          }
        })
      })
    }
  }
};
</script>
